package cn.bigway.app.weavingdesigner.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;

import cn.bigway.app.weavingdesigner.conf.WdConf;
import cn.bigway.app.weavingdesigner.swing.WdFrame;
import cn.bigway.app.weavingdesigner.swing.WdPanel;
import cn.bigway.app.weavingdesigner.type.WdViewTabType;
import cn.bigway.app.weavingdesigner.weaving.WdWeavingDoc;

public final class WdMainFrame extends WdFrame {

	private static final long serialVersionUID = 6009616723918150758L;

	private static final WdMainFrame frame;

	private static final WdPanel topPanel, bottomPanel, centerPanel, menuPanel,
			toolPanel, viewPanel, docPanel;

	private static final JDesktopPane docDesktopPane;

	private static final JTabbedPane viewTabbedPane;

	private static final JSplitPane splitPane;

	private WdMainFrame() {
		//
	}

	public static WdMainFrame getInstance() {
		return frame;
	}

	public void addWeavingDoc(WdWeavingDoc doc) {
		docDesktopPane.add(doc);
		doc.pack();
		doc.setVisible(true);
	}

	public void addViewTab(WdViewTab viewTab) {
		if (null == viewTab) {
			return;
		}
		viewTabbedPane.addTab(viewTab.getTitle(), viewTab.getIcon(), viewTab,
				viewTab.getTip());
	}

	static {
		frame = new WdMainFrame();

		topPanel = new WdPanel();
		centerPanel = new WdPanel();
		bottomPanel = new WdPanel();

		frame.getContentPane().add(topPanel, BorderLayout.NORTH);
		frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
		frame.getContentPane().add(bottomPanel, BorderLayout.SOUTH);

		menuPanel = new WdPanel();

		menuPanel.add(WdMenuBar.getInstance());
		toolPanel = new WdPanel();
		toolPanel.add(WdToolBar.getInstance());

		topPanel.add(menuPanel, BorderLayout.NORTH);
		topPanel.add(toolPanel, BorderLayout.CENTER);

		viewPanel = new WdPanel();
		viewTabbedPane = new JTabbedPane();
		viewPanel.add(viewTabbedPane, BorderLayout.CENTER);

		for (WdViewTabType type : WdConf.visibleViewTabs) {
			frame.addViewTab(WdViewTabFactory.getInstance().get(type));
		}

		docPanel = new WdPanel();
		docDesktopPane = new JDesktopPane();
		docDesktopPane.setBackground(Color.DARK_GRAY);
		docPanel.add(docDesktopPane, BorderLayout.CENTER);

		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, viewPanel,
				docPanel);
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerLocation(0.3);
		splitPane.setDividerSize(10);

		centerPanel.add(splitPane);

		bottomPanel.add(new JLabel("bottom"));

		frame.addWindowListener(new WdMainFrameListener());
	}

}

class WdMainFrameListener extends WindowAdapter {
	@Override
	public void windowClosing(WindowEvent event) {
		// TODO Auto-generated method stub
		super.windowClosing(event);
	}
}